package org.kms.Controllers;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.kms.dao.garden_listDao;
import org.kms.pojo.Garden_list;
import org.kms.pojo.KmsRole;
import org.kms.pojo.KmsUser;
import org.kms.dao.kmsUserDao;
import org.kms.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/kmsuser")
@CrossOrigin(value = "*")
public class Yonghu {

    @Autowired
    private kmsUserDao kmsUserDao;
    @Autowired
    private garden_listDao garden_listDao;

    @RequestMapping("/all")
    @ResponseBody
    public ResponseResult<List<KmsUser>> list(@RequestParam(defaultValue = "1") int page, String cha, @RequestParam(defaultValue = "10") int limit, KmsUser condition){
        Map<String,Object> map = new HashMap<>();
        if(cha!=null && !cha.equals("")){
            String[] c = cha.split(",");
            List<KmsRole> li = new ArrayList<>();
            for(String shu:c){
                KmsRole role = new KmsRole();
                role.setRoleId(Integer.parseInt(shu));
                li.add(role);

            }
            condition.setKmsRoles(li);
        }

        Page<KmsUser> pager = PageHelper.startPage(page,limit);
        List<KmsUser> list = kmsUserDao.getConditionList(condition);

        return new ResponseResult(list, pager.getTotal());
    }

    @RequestMapping("/photo")
    @ResponseBody
    public ResponseResult photo(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception{
        String pathRoot = "F:\\Users\\Administrator\\IdeaProjects\\Kms-Project\\Project\\KindergartenMS\\KindergartenMS-Ui\\src\\main\\webapp\\images\\user";
        /*   String str = "";*/
        /*for (int i=0;i<file.length;i++) {*/
        if(!file.isEmpty()){
            String filename = file.getOriginalFilename();
            File dest = new File(pathRoot + File.separator + filename);
            file.transferTo(dest);
            /*str+=filename;*/
                /*if (i<file.length-1){
                    str+=",";
                }*/
        }

        return ResponseResult.createSuccessResult();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int id) throws Exception{
        kmsUserDao.delete(id);
        kmsUserDao.del(id);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] id)throws Exception{
        kmsUserDao.batchDelete(id);
        kmsUserDao.batch(id);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(KmsUser kmsUser,String roleIds)throws Exception{

        Md5Hash md5Hash = new Md5Hash(kmsUser.getPassword(),kmsUser.getSalt());
        kmsUser.setPassword(md5Hash.toString());

        kmsUserDao.add(kmsUser);
        String[] array = roleIds.split(",");
        int[] ids = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ids[i] = Integer.parseInt(array[i]);
        }
        kmsUserDao.addUserRole(kmsUser.getUserId(),ids);

        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<Garden_list> all()throws Exception{
        return garden_listDao.getAll();
    }

    @RequestMapping("/update")
    @ResponseBody
    private ResponseResult update(KmsUser kmsUser,String roleIds)throws Exception{
        kmsUserDao.update(kmsUser);
        kmsUserDao.del(kmsUser.getUserId());
        String[] array = roleIds.split(",");
        int[] ids = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ids[i] = Integer.parseInt(array[i]);
        }
        kmsUserDao.addUserRole(kmsUser.getUserId(),ids);

        return ResponseResult.createSuccessResult();
    }



}
